package com.thinkgem.jeesite.modules.tb_message.webSocket;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import com.thinkgem.jeesite.common.security.shiro.session.SessionDAO;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm.Principal;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
/**
 * 创建socket拦截器
 * @author zhangYangZe
 *
 */
public class WebSocketHandshakeInterceptor implements HandshakeInterceptor {

	private static Logger logger = LoggerFactory.getLogger(HandshakeInterceptor.class);
	
	@Autowired
	private SessionDAO sessionDAO;
	
	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
			Map<String, Object> attributes) throws Exception {
		// TODO socket拦截器
		logger.debug("socket interceptor...");
		
		// 解决The extension [x-webkit-deflate-frame] is not supported问题
        if (request.getHeaders().containsKey("Sec-WebSocket-Extensions")) {
            request.getHeaders().set("Sec-WebSocket-Extensions",
                    "permessage-deflate");
        }
        System.out.println("Before Handshake");
        UserInstance userInstance = UserInstance.getInstance();
        Principal principal = userInstance.getPrincipal();
        User user = UserUtils.getByLoginName(principal.getLoginName());
        if (user != null) {
        	 //使用userName区分WebSocketHandler，以便定向发送消息
            String userName = user.getId();
            attributes.put(User.WEBSOCKET_USERNAME,userName);
        }
       /* if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
            String aaa =((ServletServerHttpRequest) request).getServletRequest().getParameter("username");
            User user = UserUtils.getUser();
            System.out.println("**************"+user.getLoginName());
            HttpSession session = servletRequest.getServletRequest().getSession(false);
            if (session != null) {
                //使用userName区分WebSocketHandler，以便定向发送消息
                String userName = (String) session.getAttribute(Constants.SESSION_USERNAME);
                attributes.put(Constants.WEBSOCKET_USERNAME,userName);
            }
        }*/
		return true;
	}

	@Override
	public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
			Exception exception) {
		// TODO Auto-generated method stub
		System.out.println("After Handshake");
       // this.afterHandshake(request, response, wsHandler, exception);
	}

}
